#include <jni.h>
#include <android/log.h>
#include <GLES3/gl3.h>
#include "./graphic/RectangleRenderer.h"
#include "./graphic/RectangleColorRender.h"
#include "./graphic/CubeRenderer.h"

#define LOG_TAG "LabelSimple"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

ShapeRenderer *rendererPtr;

//// 立方体的顶点数据
//GLfloat mVertices[] = {
//        // 前面
//        -0.5f, -0.5f,  0.5f,
//        0.5f, -0.5f,  0.5f,
//        0.5f,  0.5f,  0.5f,
//        -0.5f,  0.5f,  0.5f,
//        // 后面
//        -0.5f, -0.5f, -0.5f,
//        -0.5f,  0.5f, -0.5f,
//        0.5f,  0.5f, -0.5f,
//        0.5f, -0.5f, -0.5f,
//        // 左侧
//        -0.5f, -0.5f, -0.5f,
//        -0.5f, -0.5f,  0.5f,
//        -0.5f,  0.5f,  0.5f,
//        -0.5f,  0.5f, -0.5f,
//        // 右侧
//        0.5f, -0.5f, -0.5f,
//        0.5f,  0.5f, -0.5f,
//        0.5f,  0.5f,  0.5f,
//        0.5f, -0.5f,  0.5f,
//        // 顶部
//        -0.5f,  0.5f, -0.5f,
//        -0.5f,  0.5f,  0.5f,
//        0.5f,  0.5f,  0.5f,
//        0.5f,  0.5f, -0.5f,
//        // 底部
//        -0.5f, -0.5f, -0.5f,
//        0.5f, -0.5f, -0.5f,
//        0.5f, -0.5f,  0.5f,
//        -0.5f, -0.5f,  0.5f,
//};
//
//// 立方体的索引数据
//GLushort mIndices[] = {
//        0,  1,  2,  2,  3,  0,   // 前面
//        4,  5,  6,  6,  7,  4,   // 后面
//        8,  9, 10, 10, 11,  8,   // 左侧
//        12, 13, 14, 14, 15, 12,  // 右侧
//        16, 17, 18, 18, 19, 16,  // 顶部
//        20, 21, 22, 22, 23, 20   // 底部
//};
//
//GLuint program;
//GLuint vbo, ibo;
//GLuint positionLoc;
//
//// 初始化 OpenGL ES 环境
//void initOpenGL() {
//    glClearColor(1.0,1,1.0,1.0);
//    glClearDepthf(1.0);
//    //启用深度测试
//    glEnable(GL_DEPTH_TEST);
//    //设置深度测试函数为“小于或等于”
//    glDepthFunc(GL_LEQUAL);
//
//    const char* vertexShaderSource =
//            "#version 300 es\n"
//            "layout(location = 0) in vec4 a_Position;\n"
//            "void main() {\n"
//            "    gl_Position = a_Position;\n"
//            "}\n";
//
//    const char* fragmentShaderSource =
//            "#version 300 es\n"
//            "precision mediump float;\n"
//            "out vec4 fragColor;\n"
//            "void main() {\n"
//            "    fragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
//            "}\n";
//
//    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
//    glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
//    glCompileShader(vertexShader);
//
//    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
//    glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
//    glCompileShader(fragmentShader);
//
//    program = glCreateProgram();
//    glAttachShader(program, vertexShader);
//    glAttachShader(program, fragmentShader);
//    glLinkProgram(program);
//
//    glDeleteShader(vertexShader);
//    glDeleteShader(fragmentShader);
//
//    glGenBuffers(1, &vbo);
//    glBindBuffer(GL_ARRAY_BUFFER, vbo);
//    glBufferData(GL_ARRAY_BUFFER, sizeof(mVertices), mVertices, GL_STATIC_DRAW);
//
//    glGenBuffers(1, &ibo);
//    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
//    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(mIndices), mIndices, GL_STATIC_DRAW);
//
//    positionLoc = glGetAttribLocation(program, "a_Position");
//}
//
//// 绘制立方体
//void drawCube() {
//    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
//    glUseProgram(program);
//
//    glBindBuffer(GL_ARRAY_BUFFER, vbo);
//    glEnableVertexAttribArray(positionLoc);
//    glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
//
//    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
//    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, nullptr);
//
//    glDisableVertexAttribArray(positionLoc);
//}


#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_andryu_opengl_cpp_jni_LabelSimpleHelper
 * Method:    initRender
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_andryu_opengl_jni_LabelSimpleHelper_initRender(JNIEnv *env, jclass clazz, jint type) {
    if (type == 1){
        rendererPtr = new RectangleRenderer();
    } else if (type == 2){
        rendererPtr = new RectangleColorRender();
    } else if (type == 3){
        rendererPtr = new CubeRenderer();
    }

}

/*
 * Class:     com_andryu_opengl_cpp_jni_LabelSimpleHelper
 * Method:    onSurfaceCreated
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_andryu_opengl_jni_LabelSimpleHelper_onSurfaceCreated
        (JNIEnv *env, jclass object) {
    ShapeRenderer *renderer = reinterpret_cast<ShapeRenderer *>(rendererPtr);
    if (renderer != nullptr) {
        renderer->onSurfaceCreated();
    }else{
        LOGD("onSurfaceCreated renderer is Null");
    }
//initOpenGL();
}

/*
 * Class:     com_andryu_opengl_cpp_jni_LabelSimpleHelper
 * Method:    onSurfaceChanged
 * Signature: (II)V
 */
JNIEXPORT void JNICALL Java_com_andryu_opengl_jni_LabelSimpleHelper_onSurfaceChanged
        (JNIEnv *env, jclass object, jint width, jint height) {
    glViewport(0, 0, width, height);
}

/*
 * Class:     com_andryu_opengl_cpp_jni_LabelSimpleHelper
 * Method:    onDrawFrame
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_andryu_opengl_jni_LabelSimpleHelper_onDrawFrame
        (JNIEnv *env, jclass object) {
    ShapeRenderer *renderer = reinterpret_cast<ShapeRenderer *>(rendererPtr);
    if (renderer != nullptr) {
        renderer->onDrawFrame();
    }else{
        LOGD("onDrawFrame renderer is Null");
    }
//drawCube();
}

JNIEXPORT void JNICALL Java_com_andryu_opengl_jni_LabelSimpleHelper_destroy(JNIEnv *env, jclass clazz) {
    ShapeRenderer* renderer = reinterpret_cast<ShapeRenderer*>(rendererPtr);
    delete renderer;
}

#ifdef __cplusplus
}
#endif
